package com.cloudlead.customerAction.service.impl.gps;
import com.cloudlead.commonData.service.BaseCarService;
import com.cloudlead.commonData.service.BaseGpsService;
import com.cloudlead.commonData.vo.BaseCar;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseGps;
import com.cloudlead.customerAction.service.GpsSer;
import com.cloudlead.customerAction.service.impl.gps.util.HttpGps;
import com.cloudlead.customerAction.service.impl.gps.util.nbjd.FuanInterface;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Will on 2015/11/24.
 */
@Service
public class GpsSerImpl implements GpsSer{
    @Autowired
    BaseGpsService baseGpsService;
    @Autowired
    BaseCarService baseCarService;
    @Override
    public Object getGpsInfo(String gpsName, String plate) {
        BaseCar baseCar = new BaseCar();
        baseCar.setLicencePlate(plate);
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(baseCarService.findOne(baseCar).getBaseCompany().getId());
        BaseGps baseGps  = new BaseGps();
        baseGps.setGpsName(gpsName);
        baseGps.setFleet(baseCompany);
        BaseGps baseGps1 = baseGpsService.findOne(baseGps);
        Map map = new HashMap();
            if(baseGps1  == null){
                map.put("msg","未找到该车辆所属GPS公司信息");
                return map;
            }
        HttpGps httpGps = new HttpGps();
            switch(gpsName){
            case "上海英迪":
                try {
                    String str=  httpGps.getGpsInfo0(baseGps1.getUrl(), plate);
                    if(str==null||"null".equals(str)){
                        map.put("msg","暂无GPS信息反馈");
                        return map;
                    }
                    JSONArray jsonArray = JSONArray.fromObject(str);
                    JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(0));
                    map.put("carno",jsonObject.getString("carno"));
                    map.put("lon",jsonObject.getString("lon"));
                    map.put("lat",jsonObject.getString("lat"));
                    map.put("speed", jsonObject.getString("speed"));
                    map.put("recvtime", jsonObject.getString("recvtime"));
                    map.put("msg",null);
                    return map;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
                case "上海中安-富安":
                    try {
                        String stkey =  httpGps.getGpsInfo0(baseGps1.getUrl(), "version=1&method=vLoginSystem&name="+plate+"&pwd=000000");
                        JSONObject st = JSONObject.fromObject(stkey);
                        st.getString("success");
                        JSONObject ob = JSONObject.fromObject(stkey);
                        ob.getString("vid");
                        ob.getString("vKey");
                        String latn = httpGps.getGpsInfo0(baseGps1.getUrl(),"version=1&method=loadLocation&vid="+ob.getString("vid")+"&vKey="+ob.getString("vKey")+"");
                        JSONObject ob_1 = JSONObject.fromObject(latn);
                        String  ob_2 = ob_1.getString("locs");
                        JSONArray arry = JSONArray.fromObject(ob_2);
                        for(int i=0;i<arry.size();i++){
                            JSONObject jec = arry.getJSONObject(i);
                            map.put("carno",jec.getString("name"));
                            map.put("lon",jec.getString("lng"));
                            map.put("lat",jec.getString("lat"));
                            map.put("speed", jec.getString("speed"));
                            map.put("recvtime", jec.getString("recvtime"));
                            map.put("msg", null);
                            return map;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        map.put("msg", "暂无GPS信息反馈");
                        return map;
                    }
                break;
                case "嘉达科技":
                    try{
                    JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
                    svr.setServiceClass(FuanInterface.class);
                    svr.setAddress("http://116.228.138.242:8081/NBJDInterface?wsdl");
                    FuanInterface service = (FuanInterface)svr.create();
                    List<String> str =  service.carInformationsGet("jinnoo", "123", plate);
                        map.put("carno",plate);
                        map.put("lon",str.get(0));
                        map.put("lat",str.get(1));
                        map.put("speed", str.get(2));
                        map.put("recvtime", str.get(3));
                        map.put("msg",null);
                        return  map;
                    }catch (Exception e){
                        e.printStackTrace();
                        map.put("msg","暂无GPS信息反馈");
                        return map;
                    }
            }
        map.put("msg","暂无GPS信息反馈");
        return map;
    }
}
